package com.education.monitoring;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import java.sql.*;

/**
 * Created by zhangshijie on 2015/12/13.
 */
public class DBMonitoring extends BaseMonitoring {

	private JdbcTemplate jdbcTemplate;

	private String testSql;

	/**
	 * 检测心跳
	 */
	@Override
	boolean checkHeartbeat() {
		try {
			final boolean[] result = {false};
			jdbcTemplate.query(testSql, new RowCallbackHandler() {
				public void processRow(ResultSet rs) throws SQLException {
					if (rs.getInt(1) == 1) {
						result[0] = true;
					}
				}
			});

			return result[0];
		} catch (Exception e) {
			e.printStackTrace();
		}

		return false;
	}

	public void setTestSql(String testSql) {
		this.testSql = testSql;
	}

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
}